package com.hb.unic.core.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;

/**
 * request和response的工具类
 *
 * @version v0.1, 2020/7/30 11:02, create by huangbiao.
 */
@Slf4j
public class ServletUtils {

    /**
     * 获取请求参数集合，适用于get请求、表单方式application/x- www-form-urlencoded
     *
     * @return 参数集合
     */
    public static Map<String, Object> getParameterMap(HttpServletRequest request) {
        Map<String, Object> param = new HashMap<>();
        Enumeration e = request.getParameterNames();
        // 循环获取参数
        while (e.hasMoreElements()) {
            String paramterName = e.nextElement().toString();
            param.put(paramterName, request.getParameter(paramterName));
        }
        return param;
    }

    /**
     * 响应给客户端的信息
     *
     * @param response HttpServletResponse对象
     * @param json json格式信息
     */
    public static void writeResponse(HttpServletResponse response, String json) throws IOException {
        // 重置response
        response.reset();
        // 设置编码格式
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        try (PrintWriter pw = response.getWriter()) {
            pw.write(json);
            pw.flush();
        }
    }

    /**
     * 创建filter
     *
     * @param filter 具体的filter
     * @param urlPatterns 拦截路径
     * @param filterName 过滤器名称
     * @param enable 是否自动注册
     * @param order 顺序
     * @return FilterRegistrationBean
     */
    public static <T extends Filter> FilterRegistrationBean<T> createFilter(T filter, String urlPatterns,
        String filterName, boolean enable, int order) {
        FilterRegistrationBean<T> registration = new FilterRegistrationBean<>();
        // 注入过滤器
        registration.setFilter(filter);
        // 设置拦截路径
        registration.addUrlPatterns(urlPatterns);
        // 设置过滤器名称
        registration.setName(filterName);
        // 是否自动注册
        registration.setEnabled(enable);
        // 顺序
        registration.setOrder(order);
        log.info("Filter[{}] register complete, urlPatterns: [{}], order={}", filterName, urlPatterns, order);
        return registration;
    }

}
